<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */

require_once ROOT_PATH.'/Abstract/base_abstract.php';

abstract class front_abstract extends base_abstract
{
    function __construct($controller)
    {
        parent::__construct();
        mb_internal_encoding("UTF-8");
        if (!isset($_SESSION['user']['id'])) {
            if (@$_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") {
                exit('0');
            } else {
                $this->redirect('login.html');
            }
        }

        $info = _model('account')->read(array('id'=>$_SESSION['user']['account_id']));
        
        // $this->oauth_token = $info['oauth_token'];
        // $this->oauth_token_secret = $info['oauth_token_secret'];
		$this->access_token = $info['access_token'];

        $this->weibo_info = $this->api->getUserInfo($this->access_token);

        if ($controller == 'index') {
            if (_GET('t') == 4) {
                $_SESSION['user']['friends_count'] = $this->weibo_info['friends_count'];
                _model('user')->update(array('id'=>$_SESSION['user']['id']), array('friends_count'=>$this->weibo_info['friends_count']));
            } elseif (_GET('t') == 5) {
                $_SESSION['user']['followers_count'] = $this->weibo_info['followers_count'];
                _model('user')->update(array('id'=>$_SESSION['user']['id']), array('followers_count'=>$this->weibo_info['followers_count']));
            }
        }

        $tixing = array(
            'followers_count' => $this->weibo_info['followers_count'] - $_SESSION['user']['followers_count'],
            'friends_count' => $this->weibo_info['friends_count'] - $_SESSION['user']['friends_count']
        );

        $this->smarty->assign('tixing', $tixing);

        $this->accounts = array();

        if ($_SESSION['user']['parent_id'] == 0) {
            $data  = _model('user')->getlist(array('parent_id'=>$_SESSION['user']['id']));
            $ids = array();
            foreach ($data as $key => $val) {
                $ids[] = $val['account_id'];
            }
        } else {
            $ids = array_filter(explode(',', $_SESSION['user']['accounts']));
        }
    
        if ($ids) {
            $tokens = _model('account')->getlist("WHERE id IN ('".implode("', '", $ids)."') AND is_bind=1");
            foreach($tokens as $val){
                $c = $this->api->getUserInfo($val['oauth_token'], $val['oauth_token_secret']);
                if (empty($c['id'])) {
                    _model('account')->update(array('id'=>$val['id']), array('is_bind'=>0));
                    continue;
                }
				$c['oauth_token'] = $val['oauth_token'];//quq add
				$c['oauth_token_secret'] = $val['oauth_token_secret'];//quq add
                $this->accounts[] = $c;
            }
        }else{
            $this->accounts = array();
        }

        $notice = _model('config')->read(array('cf_key'=>'notice'));
        $notice['short_value'] = $notice['value'];
        if (mb_strlen($notice['value']) > 40) {
            $notice['short_value'] = mb_substr($notice['value'], 0, 37).'...';
        }
        $this->smarty->assign('notice', $notice);
        $this->smarty->assign('accounts', $this->accounts);
        $this->smarty->assign('weibo_info', $this->weibo_info);
    }

    function replace_content($content)
    {
        static $face = array();
        if (!$face) {
            $t = $this->api->emotions($this->access_token);
            $face = array();
            foreach ($t as $key => $val) {
                $face[$val['phrase']] = '<img src="'.$val['url'].'" />';
            }
        }
		$content = preg_replace('@(http://t.cn/\w+)@is' , "<a href=\"\\1\">\\1</a>" , $content);
        $content = preg_replace("/(@([^:,\.;\s:]+))/i", "<a href=\"http://weibo.cn/n/\\2\" target=\"_blank\">\\1</a>", $content);
        $content = preg_replace("/(\#(.+?)\#)/i", "<a href=\"http://weibo.cn/k/\\2\" target=\"_blank\">\\1</a>", $content);
        $content = strtr($content, $face);
        return  $content;
    }
}


?>